<template>
  <div class="bg">
    <div class="login-container">
      <el-card header="请登录" class="login-card">
        <el-form @submit.native.prevent="login">
          <el-form-item label="用户名">
            <el-input v-model="model.username"></el-input>
          </el-form-item>
          <el-form-item label="密码">
            <el-input type="password" v-model="model.password"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" native-type="submit">登录</el-button>
            <el-button type="primary" @click="model={}" style="float: right;">重置</el-button>
            <p class="tips">账号未注册时会自动注册</p>
          </el-form-item>
        </el-form>
      </el-card>
    </div>
  </div>
</template>

<script>
export default {
  data () {
    return {
      model: {}
    }
  },
  methods: {
    async login () {
      // console.log(this.model)
      const res = await this.$http.post('/login', this.model)
      // console.log(res)
      // sessionStorage 浏览器关闭后清除
      localStorage.token = res.data.token
      localStorage.username = res.data.username
      this.$router.push('/')
      this.$message({
        type: 'success',
        message: '登录成功'
      })
    }
  }
}
</script>

<style lang="scss">
.bg {
  height: 100vh;
  background: url(http://imgs.aixifan.com/o_1c8uc9u9l1k0rs71uob1cia3bd3b.jpg) top center no-repeat;
  background-color: #283443;
  background-size: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
  .login-container {
    width: 400px;
    .tips {
      text-align: right;
      font-size: 12px;
      margin: 0;
      color: #999;
    }
  }
}
</style>
